package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Class;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.Lesson;

import cn.wolfcode.crm.mapper.ClassMapper;
import cn.wolfcode.crm.mapper.LessonMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.ILessonService;
import cn.wolfcode.crm.util.PageResult;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class LessonServiceImpl implements ILessonService {
    @Autowired
    private LessonMapper lessonMapper;

    @Autowired
    private ClassMapper classMapper;

    private Class clz;  //将该类

    public Lesson selectByPrimaryKey(Long id) {
        return lessonMapper.selectByPrimaryKey(id);
    }

    public List<Lesson> selectAll() {
        return lessonMapper.selectAll();
    }

    public PageResult quey4List(QueryObject qo) {
        Integer count = lessonMapper.query4Count(qo);
        if (count == 0){
            return PageResult.PAGERESULT_EMPTY;
        }
        List<Lesson> rows = (List<Lesson>) lessonMapper.query4List(qo);
        return new PageResult(count,rows);
    }


    /**
     * 用户获取获取课程模板
     * @return
     */
    public Workbook getImportXlsMode() {
        //创建xls
        Workbook book = new HSSFWorkbook();
        //创建工作簿
        Sheet sheet = book.createSheet();
        //设置第一行的标题
        Row row = sheet.createRow(0);
        row.createCell(0).setCellValue("日期");
        row.createCell(1).setCellValue("课程名称");
        row.createCell(2).setCellValue("备注");
        return book;
    }

    /**
     *
     * @param file
     * @param response
     * @param
     * @throws Exception
     */
    public void importXls(MultipartFile file, HttpServletResponse response) throws Exception {
        InputStream inputStream = file.getInputStream();
        //创建Excel文件,指定输入流
        Workbook wb = new HSSFWorkbook(inputStream);
        Sheet sheet = wb.getSheetAt(0);
        //获取最后一行的索引
        int lastRowNum = sheet.getLastRowNum();
        for (int i = 1; i <= lastRowNum; i++) {
            Lesson lesson = new Lesson();
            Row row = sheet.getRow(i);
            //日期转换
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            //获取单元格
            Cell cell = row.getCell(0);
            //设置单元格类型
            cell.setCellType(CellType.STRING);

            String value = cell.getStringCellValue();
            Date date = format.parse(value);
            lesson.setLessonDate(date);
            lesson.setName(row.getCell(1).getStringCellValue());
            //备注，excel里面不能为空
            String sign = row.getCell(2).getStringCellValue();
            lesson.setSign(sign);
            //====================================================================

            lesson.setClassManager(clz.getClassManager());       //设置班主任
            lesson.setTeacher(clz.getTeacher());                //设置任课老师
            lesson.setClz(clz);                     //设置班级
            lesson.setClassroom(clz.getClassroom());    //设置课室
            System.out.println(lesson);
            lessonMapper.insert(lesson);
        }
    }

    public void getClassById(Long clzId) {
        this.clz = classMapper.selectByPrimaryKey(clzId);

    }
}
